package cn.tedu.ivos.approval.service.impl;

import cn.tedu.ivos.approval.mapper.ApplicationMapper;
import cn.tedu.ivos.approval.mapper.AuditMapper;
import cn.tedu.ivos.approval.pojo.dto.ApplicationQueryParam;
import cn.tedu.ivos.approval.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.approval.pojo.entity.Application;
import cn.tedu.ivos.approval.pojo.entity.Audit;
import cn.tedu.ivos.approval.pojo.vo.ApplicationVO;
import cn.tedu.ivos.approval.service.ApplicationService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.base.enums.AuditStatusEnum;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Transactional(rollbackFor = Exception.class,
               timeout = 60,
               isolation = Isolation.READ_COMMITTED,
               propagation = Propagation.REQUIRED)
@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    private ApplicationMapper applicationMapper;

    @Autowired
    private AuditMapper auditMapper;

    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQueryParam applicationQuery) {
        return applicationMapper.selectApplication(applicationQuery);
    }

    @Override
    public void save(ApplicationSaveParam applicationSaveParam) {
        Application application=new Application();
        BeanUtils.copyProperties(applicationSaveParam,application);
        //新增申请单,初始状态为"已发起"(这里有个重点就是状态机的变化)
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());
        System.out.println(application);
        applicationMapper.insert(application);
        //向审核人发起申请(审核人有多个，应该向审核表中写入多条数据)
        //谁来操作审核数据(一种方式在这里直接调用审核数据层对象，一种情况是调用审核服务对象)
        String auditUser=application.getAuditUsers();//[{"userId":101,"username":"shaoyun"}]
        ObjectMapper objectMapper=new ObjectMapper();
        try {
            List<Map<String, Object>> list = objectMapper.readValue(auditUser, List.class);
            System.out.println(list);
            for (int i = 0; i < list.size(); i++) {
                Audit audit = new Audit();
                audit.setApplicationId(application.getId());
                audit.setAuditUserId(Long.parseLong(list.get(i).get("userId").toString()));
                audit.setAuditSort(i);
                if (i == 0) {
                    audit.setAuditStatus(AuditStatusEnum.MY_PENDING.getCode());//10
                } else {
                    audit.setAuditStatus(AuditStatusEnum.PENDING.getCode());
                }
                audit.setCreateTime(new Date());
                //向审批单中添加审批单信息
                auditMapper.insert(audit);
            }
          }catch(Exception e){e.printStackTrace();}
        }

    @Override
    public void distributeVehicle(Long applicationId, Long vehicleId) {
        Application application=new Application();
        application.setId(applicationId);
        application.setVehicleId(vehicleId);
        application.setUpdateTime(new Date());
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        applicationMapper.update(application);

        Vehicle vehicle=new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setUpdateTime(new Date());
        vehicle.setStatus("2");
        vehicleMapper.update(vehicle);
    }
    @Override
    public void backVehicle(Long applicationId, Long vehicleId) {
        //修改申请单状态
        Application application=new Application();
        application.setId(applicationId);
        //application.setVehicleId(null);
        application.setUpdateTime(new Date());
        application.setStatus(ApplicationStatusEnum.END.getCode());
        applicationMapper.update(application);
        //修改车辆的状态
        Vehicle vehicle=new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setUpdateTime(new Date());
        vehicle.setStatus("1");
        vehicleMapper.update(vehicle);
    }

    @Override
    public void cancel(Long id) {
        Application application=new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        auditMapper.deleteAuditByApplicationId(id);
    }
}
